#!/bin/bash
# $Id: media-install, v 1.0, 2010/06/01, gn Exp $
#
# Complete install of Media Librarys and Tools
#
# Copyright (C) 2011, Sascha Heim
#
# media-install is free software ; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation ; either version 2 of the License, or
# (at your option) any later version.
#
# media-install is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY ; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program ; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
# Speed up build time using multpile processor cores.

apt_get_multimedia_repo='deb http://www.debian-multimedia.org lenny main'
apt_get_sources_list='/etc/apt/sources.list'
installAll=true

export cpu=`grep -c ^processor /proc/cpuinfo 2>/dev/null`
export GLOBIGNORE='*.sh'

green() { echo -e "\e[1;32m$1\e[0;39;49m"; }

brown() { echo -e "\e[0;33m$1\e[0;39;49m"; }

error() { echo -e "\e[1;31m$1\e[0;39;49m"; }

reset() { echo -e "\033[0m"; }

welcome()
{
	green "+================================================+"
	green "|     media-install - Makes Your Live Easier     |"
	green "+================================================+"
}

install_dir()
{
	if $installAll ; then
		installdir="/usr"
		export install_directory=$installdir
		rm -r mediaInstall
		mkdir mediaInstall && cd mediaInstall
	else
		printf "\n"
		brown "+================================================+"
		brown "|      Install Directory for all Programms       |"
		brown "+================================================+"
		printf "Which is your Install Directory [e.g /usr] ? Default is -> /usr "
		printf "\n"
		
		read installdir
		if [ -z "$installdir" ]; then
			installdir="/usr"
		fi
		export install_directory=$installdir
		green "Your Install Directory is -> '$install_directory'"
		rm -r mediaInstall
		mkdir mediaInstall && cd mediaInstall
	fi
}

install_all()
{
	printf "\n"
	brown "+================================================+"
	brown "|      Install All Programs                      |"
	brown "+================================================+"
	printf "Would you like install all Programs automatically ? [y/N]: "

	read _install_All
	if [ "$_install_All" = "y" -o "$_install_All" = "Y" ]; then
		installAll=true
	else
		installAll=false
	fi
}

add_media_sources()
{
	if $installAll ; then
		grep_output="$(grep "$apt_get_multimedia_repo" $apt_get_sources_list)"
		if [ -z "$grep_output" ]; then
			echo "deb http://www.debian-multimedia.org lenny main" >> /etc/apt/sources.list;
			apt-get update
			clear
		fi
	else
		printf "Would you add to your sources.list these Entry ? [y/N]: " brown "http://www.debian-multimedia.org"
		read add
		if [ "$add" = "y" -o "$add" = "Y" ]; then
		
			grep_output="$(grep "$apt_get_multimedia_repo" $apt_get_sources_list)"

			if [ -z "$grep_output" ]; then
				echo "deb http://www.debian-multimedia.org lenny main" >> /etc/apt/sources.list;
				apt-get update
				clear
			else
				echo "Repo is found in Sources dont Touch it Again... ";
			fi
			
		else
			error "Attention some Libraries will be needed"
			error "Without these Add you becomes some Errors"
		fi
	fi
}

install_dev_dependencies()
{
	if $installAll ; then 
		apt-get install libpcre3-dev rpm make openssl zlibc zlib1g-dev build-essential libjpeg-progs libjpeg62 libjpeg62-dev libsdl1.2-dev subversion libpng12-dev mc libdts-dev libtool patch automake zip unzip gettext libfreetype6-dev bzip2 gzip autoconf sudo git-core yasm libgsm1-dev libopencore-amrnb-dev libopencore-amrwb-dev
	else
		printf "Do you want Install the requirred Developement Headers ? [y/N]: "
		read input
		
		if [ "$input" = "y" -o "$input" = "Y" ]; then
			apt-get install libpcre3-dev rpm make openssl zlibc zlib1g-dev build-essential libjpeg-progs libjpeg62 libjpeg62-dev libsdl1.2-dev subversion libpng12-dev mc libdts-dev libtool patch automake zip unzip gettext libfreetype6-dev bzip2 gzip autoconf sudo git-core yasm libgsm1-dev libopencore-amrnb-dev libopencore-amrwb-dev
		else
			error "Headers are not Installed"
		fi
	fi
}

install_gpac()
{
	if $installAll ; then
	  svn co https://gpac.svn.sourceforge.net/svnroot/gpac/trunk/gpac gpac
		cd gpac/
		chmod -R 755 configure
		./configure --prefix=$install_directory
		
		make -j $cpu
		make install
		make install-lib
		cd ..
	else
		printf "Would you like install the GPAC-Framework ? [y/N]: "
		read gpacin
		if [ "$gpacin" = "y" -o "$gpacin" = "Y" ]; then
			svn co https://gpac.svn.sourceforge.net/svnroot/gpac/trunk/gpac gpac
			cd gpac/
			chmod -R 755 configure
			./configure --prefix=$install_directory
			
			make -j $cpu
			make install
			make install-lib
			cd ..
			green "Installation of GPAC-Framework Successfully"
		else
			error "Installation of GPAC-Framework Aborted"
		fi
	fi
}

install_flvtool()
{
	if $installAll ; then 
		wget http://rubyforge-files.ruby-forum.com/flvtool2/flvtool2-1.0.6.tgz
		tar -zxvf  flvtool2-1.0.6.tgz
		cd flvtool2-1.0.6/
		ruby setup.rb config
		ruby setup.rb setup
		ruby setup.rb install
	else
		printf "Would you like install FLVTOOL-2 ? [y/N]: "
		read flvin
		if [ "$flvin" = "y" -o "$flvin" = "Y" ]; then
			wget http://rubyforge-files.ruby-forum.com/flvtool2/flvtool2-1.0.6.tgz
			tar -zxvf  flvtool2-1.0.6.tgz
			cd flvtool2-1.0.6/
			ruby setup.rb config
			ruby setup.rb setup
			ruby setup.rb install
			green "Installation of FLVTOOL-2 Successfully"
		else
			error "Installation of FLVTOOL-2 Aborted"
		fi
	fi
}

install_x264()
{
	if $installAll ; then 
		git clone git://git.videolan.org/x264.git
		cd x264
		./configure --enable-shared --prefix=$install_directory
		
		make -j $cpu
		make install
		cd ..
	else
		printf "Would you like install X264 ? [y/N]: "
		read x264in
		if [ "$x264in" = "y" -o "$x264in" = "Y" ]; then
			git clone git://git.videolan.org/x264.git
			cd x264
			./configure --enable-shared --prefix=$install_directory
			
			make -j $cpu
			make install
			cd ..
			green "Installation of X264 Successfully"
		else
			error "Installation of X264 Aborted"
		fi
	fi
}

ffmpeg_config()
{
	if $installAll ; then
		gpl="--enable-gpl"
		gpl3="--enable-version3"
		nonfree="--enable-nonfree"
		shared="--enable-shared"
		postproc="--enable-postproc"
		avfilter="--enable-avfilter"
		pthreads="--enable-pthreads"
		pthreads="--enable-pthreads"
		bzlib="--enable-bzlib"
		libfaac="--enable-libfaac"
		libmp3lame="--enable-libmp3lame"
		libtheora="--enable-libtheora"
		libvorbis="--enable-libvorbis"
		libx264="--enable-libx264"
		libxvid="--enable-libxvid"
		libopencore_amrnb="--enable-libopencore-amrnb"
		libopencore-amrwb="--enable-libopencore-amrwb"
		zlib="--enable-zlib"
	else
		printf "Enable GPL ? [y/N]: "
		read gplinput
		if [ "$gplinput" = "y" -o "$gplinput" = "Y" ]; then
			gpl="--enable-gpl"
		else
			gpl=""
		fi
		
		printf "Enable GPL version 3 ? [y/N]: "
		read gplinput
		if [ "$gplinput" = "y" -o "$gplinput" = "Y" ]; then
			gpl3="--enable-version3"
		else
			gpl3=""
		fi
		
		printf "Enable nonfree ? [y/N]: "
		read nonfree_input
		if [ "$nonfree_input" = "y" -o "$nonfree_input" = "Y" ]; then
			nonfree="--enable-nonfree"
		else
			nonfree=""
		fi
		
		printf "Enable shared ? [y/N]: "
		read shared_input
		if [ "$shared_input" = "y" -o "$shared_input" = "Y" ]; then
			shared="--enable-shared"
		else
			shared=""
		fi
		
		printf "Enable postproc ? [y/N]: "
		read postproc_input
		if [ "$postproc_input" = "y" -o "$postproc_input" = "Y" ]; then
			postproc="--enable-postproc"
		else
			postproc=""
		fi
		
		printf "Enable avfilter ? [y/N]: "
		read avfilter_input
		if [ "$avfilter_input" = "y" -o "$avfilter_input" = "Y" ]; then
			avfilter="--enable-avfilter"
		else
			avfilter=""
		fi
		
		printf "Enable pthreads ? [y/N]: "
		read pthreads_input
		if [ "$pthreads_input" = "y" -o "$pthreads_input" = "Y" ]; then
			pthreads="--enable-pthreads"
		else
			pthreads=""
		fi
		
		printf "Enable x11grab ? [y/N]: "
		read x11grab_input
		if [ "$x11grab_input" = "y" -o "$x11grab_input" = "Y" ]; then
			x11grab="--enable-x11grab"
		else
			x11grab=""
		fi
		
		printf "Enable bzlib ? [y/N]: "
		read bzlib_input
		if [ "$bzlib_input" = "y" -o "$bzlib_input" = "Y" ]; then
			bzlib="--enable-bzlib"
		else
			bzlib=""
		fi
		
		printf "Enable libfaac ? [y/N]: "
		read libfaac_input
		if [ "$libfaac_input" = "y" -o "$libfaac_input" = "Y" ]; then
			libfaac="--enable-libfaac"
		else
			libfaac=""
		fi
		
		printf "Enable libmp3lame ? [y/N]: "
		read libmp3lame_input
		if [ "$libmp3lame_input" = "y" -o "$libmp3lame_input" = "Y" ]; then
			libmp3lame="--enable-libmp3lame"
		else
			libmp3lame=""
		fi
		
		printf "Enable libtheora ? [y/N]: "
		read libtheora_input
		if [ "$libtheora_input" = "y" -o "$libtheora_input" = "Y" ]; then
			libtheora="--enable-libtheora"
		else
			libtheora=""
		fi
		
		printf "Enable libvorbis ? [y/N]: "
		read libvorbis_input
		if [ "$libvorbis_input" = "y" -o "$libvorbis_input" = "Y" ]; then
			libvorbis="--enable-libvorbis"
		else
			libvorbis=""
		fi
		
		printf "Enable libx264 ? [y/N]: "
		read libx264_input
		if [ "$libx264_input" = "y" -o "$libx264_input" = "Y" ]; then
			libx264="--enable-libx264"
		else
			libx264=""
		fi
		
		printf "Enable libxvid ? [y/N]: "
		read libxvid_input
		if [ "$libxvid_input" = "y" -o "$libxvid_input" = "Y" ]; then
			libxvid="--enable-libxvid"
		else
			libxvid=""
		fi
		
		printf "Enable libvpx ? [y/N]: "
		read libvpx_input
		if [ "$libvpx_input" = "y" -o "$libvpx_input" = "Y" ]; then
			libvpx="--enable-libvpx"
		else
			libvpx=""
		fi	

		printf "Enable libopencore-amrnb ? [y/N]: "
		read libopencore_amrnb_input
		if [ "$libopencore_amrnb_input" = "y" -o "$libopencore_amrnb_input" = "Y" ]; then
			libopencore_amrnb="--enable-libopencore-amrnb"
		else
			libopencore_amrnb=""
		fi	
		
		printf "Enable libopencore-amrwb ? [y/N]: "
		read libopencore_amrwb_input
		if [ "$libopencore_amrwb_input" = "y" -o "$libopencore_amrwb_input" = "Y" ]; then
			libopencore_amrwb="--enable-libopencore-amrwb"
		else
			libopencore_amrwb=""
		fi	

		printf "Enable zlib ? [y/N]: "
		read zlib_input
		if [ "$zlib_input" = "y" -o "$zlib_input" = "Y" ]; then
			zlib="--enable-zlib"		
		else
			zlib=""
		fi
	fi
	
	./configure --prefix=$install_directory --extra-cflags=-I$install_directory/include/ --extra-ldflags=-L$install_directory/lib $libvpx $gpl $gpl3 $nonfree $shared $postproc $avfilter $pthreads $x11grab $bzlib $libfaac $libmp3lame $libtheora $libvorbis $libx264 $libxvid $libopencore_amrnb $libopencore_amrwb $zlib
}

export_ld_libery() 
{
	if $installAll ; then
		export LD_LIBRARY_PATH=/usr/local/lib:/usr/lib:$LD_LIBRARY_PATH
	else
		printf "Would you Export LD_LIBRARY_PATH ? [y/N]: "
		read int
		if [ "$int" = "y" -o "$int" = "Y" ]; then
			export LD_LIBRARY_PATH=/usr/local/lib:/usr/lib:$LD_LIBRARY_PATH
			green "LD_LIBRARY_PATH successfully Exported"
		else
			error "No LD_LIBRARY_PATH exported"
		fi
	fi
}

install_ffmpeg()
{
	if $installAll ; then
		ff_directory="./ffmpeg"
			if [ -d $ff_directory ]; then
				rm -r $ff_directory
			fi
		rm -vrf $install_directory/lib/libavcodec*
		rm -vrf $install_directory/lib/libavformat*
		rm -vrf $install_directory/lib/libavfilter*
		rm -vrf $install_directory/lib/libavutil*
		rm -vrf $install_directory/lib/libswscale*
		rm -vrf $install_directory/local/lib/libavcodec*
		rm -vrf $install_directory/local/lib/libavformat*
		rm -vrf $install_directory/local/lib/libavfilter*
		rm -vrf $install_directory/local/lib/libavutil*
		rm -vrf $install_directory/local/lib/libswscale*
		unlink $install_directory/bin/ffmpeg >/dev/null 2>&1
		unlink $install_directory/local/bin/ffmpeg >/dev/null 2>&1
		git clone git://git.videolan.org/ffmpeg.git ffmpeg
		cd ffmpeg
		sudo ldconfig
		ffmpeg_config
		make -j $cpu
		make tools/qt-faststart
		sudo make install
		sudo ldconfig
		cd ..
	else
		printf "Would you like install FFmpeg ? [y/N]: "
		read ffmpegin
		if [ "$ffmpegin" = "y" -o "$ffmpegin" = "Y" ]; then
				ff_directory="./ffmpeg"
				if [ -d $ff_directory ]; then
					rm -r $ff_directory
				fi
			rm -vrf $install_directory/lib/libavcodec*
			rm -vrf $install_directory/lib/libavformat*
			rm -vrf $install_directory/lib/libavfilter*
			rm -vrf $install_directory/lib/libavutil*
			rm -vrf $install_directory/lib/libswscale*
			rm -vrf $install_directory/local/lib/libavcodec*
			rm -vrf $install_directory/local/lib/libavformat*
			rm -vrf $install_directory/local/lib/libavfilter*
			rm -vrf $install_directory/local/lib/libavutil*
			rm -vrf $install_directory/local/lib/libswscale*
			unlink $install_directory/bin/ffmpeg >/dev/null 2>&1
			unlink $install_directory/local/bin/ffmpeg >/dev/null 2>&1
			git clone git://git.videolan.org/ffmpeg.git ffmpeg
			cd ffmpeg
			sudo ldconfig
			ffmpeg_config
			make -j $cpu
			make tools/qt-faststart
			sudo make install
			sudo ldconfig
			cd ..
			green "Installation of FFmpeg Successfully Installed"
		else
			error "Installation of FFmpeg Aborted"
		fi
	fi
}

install_lame()
{
	if $installAll ; then
		wget http://mesh.dl.sourceforge.net/sourceforge/lame/lame-3.98.4.tar.gz
		tar -zxvf lame-3.98.4.tar.gz
		cd lame-3.98.4
		./configure --prefix=$install_directory --enable-mp3x
		
		make -j $cpu
		make install
		cd ..
	else
		printf "Would you like install Lame MP3 Library ? [y/N]: "
		read lamein
		if [ "$lamein" = "y" -o "$lamein" = "Y" ]; then
			wget http://mesh.dl.sourceforge.net/sourceforge/lame/lame-3.98.4.tar.gz
			tar -zxvf lame-3.98.4.tar.gz
			cd lame-3.98.4
			./configure --prefix=$install_directory --enable-mp3x
			
			make -j $cpu
			make install
			cd ..
			green "Installation of Lame MP3 Successfully"
		else
			error "Installation of MP3 Library Aborted"
		fi
	fi
}

install_ffmpegsource()
{
	if $installAll ; then
		svn checkout http://ffmpegsource.googlecode.com/svn/trunk/ ffmpegsource
		cd ffmpegsource/
		./configure --prefix=$install_directory
			
		make -j $cpu
		make install
		cd ..
	else	
		printf "Would you like install ffmpegsource Library ? [y/N]: "
		read ffmpegsourcein
		if [ "$ffmpegsourcein" = "y" -o "$ffmpegsourcein" = "Y" ]; then
			svn checkout http://ffmpegsource.googlecode.com/svn/trunk/ ffmpegsource
			cd ffmpegsource/
			./configure --prefix=$install_directory
			
			make -j $cpu
			make install
			cd ..
			green "Installation of ffmpegsource Library Successfully"
		else
			error "Installation of ffmpegsource Library Aborted"
		fi
	fi
}

install_faad2()
{
	if $installAll ; then
		wget http://mesh.dl.sourceforge.net/sourceforge/faac/faad2-2.7.tar.gz
		tar -xvzf faad2-2.7.tar.gz
		cd faad2-2.7/
		autoreconf -vif
		./configure --prefix=$install_directory --with-mpeg4ip
		
		make -j $cpu
		make install
		cd ..
	else	
		printf "Would you like install FAAD2 Library ? [y/N]: "
		read faadin
		if [ "$faadin" = "y" -o "$faadin" = "Y" ]; then
			wget http://mesh.dl.sourceforge.net/sourceforge/faac/faad2-2.7.tar.gz
			tar -xvzf faad2-2.7.tar.gz
			cd faad2-2.7/
			autoreconf -vif
			./configure --prefix=$install_directory --with-mpeg4ip
			
			make -j $cpu
			make install
			cd ..
			green "Installation of FAAD2 Successfully"
		else
			error "Installation of FAAD2 Library Aborted"
		fi
	fi
}

install_faac()
{
	if $installAll ; then
		wget http://mesh.dl.sourceforge.net/sourceforge/faac/faac-1.28.tar.gz
		tar -xvzf faac-1.28.tar.gz
		cd faac-1.28/
		./bootstrap
		./configure --prefix=$install_directory --with-mp4v2
		
		make -j $cpu
		make install
		cd ..
	else
		printf "Would you like install FAAC Library ? [y/N]: "
		read faacin
		if [ "$faacin" = "y" -o "$faacin" = "Y" ]; then
			wget http://mesh.dl.sourceforge.net/sourceforge/faac/faac-1.28.tar.gz
			tar -xvzf faac-1.28.tar.gz
			cd faac-1.28/
			./bootstrap
			./configure --prefix=$install_directory --with-mp4v2
			
			make -j $cpu
			make install
			cd ..
			green "Installation of FAAC Successfully"
		else
			error "Installation of FAAC Library Aborted"
		fi
	fi
}

install_codecs()
{	
	if $installAll ; then
		wget http://www.mplayerhq.hu/MPlayer/releases/codecs/all-20100303.tar.bz2
		tar -xvjf all-20100303.tar.bz2
		chown -R root.root all-20100303/
		mkdir -pv $install_directory/lib/codecs/
		cp -vrf all-20100303/* $install_directory/lib/codecs/
		chmod -R 755 $install_directory/lib/codecs/
	else
		printf "Would you like install Codecs-Pack ? [y/N]: "
		read installcodecsin
		if [ "$installcodecsin" = "y" -o "$installcodecsin" = "Y" ]; then
			wget http://www.mplayerhq.hu/MPlayer/releases/codecs/all-20100303.tar.bz2
			tar -xvjf all-20100303.tar.bz2
			chown -R root.root all-20100303/
			mkdir -pv $install_directory/lib/codecs/
			cp -vrf all-20100303/* $install_directory/lib/codecs/
			chmod -R 755 $install_directory/lib/codecs/
			green "Installation of Codecs-Pack Successfully"
		else
			error "Installation of Codecs-Pack Aborted"
		fi
	fi
}

install_libogg()
{
	if $installAll ; then
		wget http://downloads.xiph.org/releases/ogg/libogg-1.2.2.tar.gz
		tar -xvzf libogg-1.2.2.tar.gz
		cd libogg-1.2.2/
		./configure --prefix=$install_directory
		
		make -j $cpu
		make install
		cd ..
	else
		printf "Would you like install LibOgg Library ? [y/N]: "
		read liboggin
		if [ "$liboggin" = "y" -o "$liboggin" = "Y" ]; then
			wget http://downloads.xiph.org/releases/ogg/libogg-1.2.2.tar.gz
			tar -xvzf libogg-1.2.2.tar.gz
			cd libogg-1.2.2/
			./configure --prefix=$install_directory
			
			make -j $cpu
			make install
			cd ..
			green "Installation of LibOgg Successfully"
		else
			error "Installation of LibOgg Library Aborted"
		fi
	fi
}

install_mplayer()
{
	if $installAll ; then
		svn checkout svn://svn.mplayerhq.hu/mplayer/trunk mplayer
		cd mplayer/
		./configure --prefix=$install_directory --codecsdir=$install_directory/lib/codecs/
			
		make -j $cpu
		make install
		cp -f etc/codecs.conf $install_directory/etc/mplayer/codecs.conf
		cd ..
	else
		printf "Would you like install Mplayer ? [y/N]: "
		read mplayerin
		if [ "$mplayerin" = "y" -o "$mplayerin" = "Y" ]; then
			svn checkout svn://svn.mplayerhq.hu/mplayer/trunk mplayer
			   cd mplayer/
				./configure --prefix=$install_directory --codecsdir=$install_directory/lib/codecs/
			
			make -j $cpu
			make install
			cp -f etc/codecs.conf $install_directory/etc/mplayer/codecs.conf
			cd ..
			green "Installation of Mplayer Successfully"
		else
			error "Installation of Mplayer Aborted"
		fi
	fi
}

install_xvid()
{
	if $installAll ; then
		wget http://downloads.xvid.org/downloads/xvidcore-1.3.2.tar.gz
		tar -xvzf xvidcore-1.3.2.tar.gz
		cd xvidcore/build/generic/
		./configure --prefix=$install_directory
			
		make -j $cpu
		make install
		cd ..
		cd ..
		cd ..
	else
		printf "Would you like install Xvid Library ? [y/N]: "
		read xvidin
		if [ "$xvidin" = "y" -o "$xvidin" = "Y" ]; then
			wget http://downloads.xvid.org/downloads/xvidcore-1.3.2.tar.gz
			tar -xvzf xvidcore-1.3.2.tar.gz
			cd xvidcore/build/generic/
			./configure --prefix=$install_directory
			
			make -j $cpu
			make install
			cd ..
			cd ..
			cd ..
			green "Installation of Xvid Successfully"
		else
			error "Installation of Xvid Library Aborted"
		fi
	fi
}

install_libvorbis()
{
	if $installAll ; then 
		wget http://downloads.xiph.org/releases/vorbis/libvorbis-1.3.2.tar.gz
		tar -xvzf libvorbis-1.3.2.tar.gz
		cd libvorbis-1.3.2
		./configure --prefix=$install_directory
		
		make -j $cpu
		make install
		cd ..
	else
		printf "Would you like install libvorbis Library ? [y/N]: "
		read libvorbisin
		if [ "$libvorbisin" = "y" -o "$libvorbisin" = "Y" ]; then
			wget http://downloads.xiph.org/releases/vorbis/libvorbis-1.3.2.tar.gz
			tar -xvzf libvorbis-1.3.2.tar.gz
			cd libvorbis-1.3.2
			./configure --prefix=$install_directory
			
			make -j $cpu
			make install
			cd ..
			green "Installation of libvorbis Successfully"
		else
			error "Installation of libvorbis Library Aborted"
		fi
	fi
}

install_vorbisTools()
{
	if $installAll ; then
		wget http://downloads.xiph.org/releases/vorbis/vorbis-tools-1.4.0.tar.gz
		tar -xvzf vorbis-tools-1.4.0.tar.gz
		cd vorbis-tools-1.4.0/
		./configure --prefix=$install_directory
			
		make -j $cpu
		make install
		cd ..
	else
		printf "Would you like install vorbis-tools ? [y/N]: "
		read vtoolsin
		if [ "$vtoolsin" = "y" -o "$vtoolsin" = "Y" ]; then
			wget http://downloads.xiph.org/releases/vorbis/vorbis-tools-1.4.0.tar.gz
			tar -xvzf vorbis-tools-1.4.0.tar.gz
			cd vorbis-tools-1.4.0/
			./configure --prefix=$install_directory
			
			make -j $cpu
			make install
			cd ..
			green "Installation of vorbis-tools Successfully"
		else
			error "Installation of vorbis-tools Aborted"
		fi
	fi
}

install_libtheora()
{
	if $installAll ; then
		wget http://downloads.xiph.org/releases/theora/libtheora-1.1.1.tar.bz2
		tar -xvjf libtheora-1.1.1.tar.bz2
		cd libtheora-1.1.1/
		./configure --prefix=$install_directory --with-ogg=$install_directory --with-vorbis=$install_directory
			
		make -j $cpu
		make install
		cd ..
	else
		printf "Would you like install libtheora ? [y/N]: "
		read libtheorain
		if [ "$libtheorain" = "y" -o "$libtheorain" = "Y" ]; then
			wget http://downloads.xiph.org/releases/theora/libtheora-1.1.1.tar.bz2
			tar -xvjf libtheora-1.1.1.tar.bz2
			cd libtheora-1.1.1/
			./configure --prefix=$install_directory --with-ogg=$install_directory --with-vorbis=$install_directory
			
			make -j $cpu
			make install
			cd ..
			green "Installation of libtheora Successfully"
		else
			error "Installation of libtheora Aborted"
		fi
	fi
}
install_re2c()
{
	if $installAll ; then
		wget http://mesh.dl.sourceforge.net/sourceforge/re2c/re2c-0.13.5.tar.gz
		tar -xvzf re2c-0.13.5.tar.gz
		cd re2c-0.13.5/
		./configure --prefix=$install_directory

		make -j $cpu
		make install
		cd ..
	else
		printf "Would you like install re2c ? [y/N]: "
		read re2cin
		if [ "$re2cin" = "y" -o "$re2cin" = "Y" ]; then
			wget http://mesh.dl.sourceforge.net/sourceforge/re2c/re2c-0.13.5.tar.gz
			tar -xvzf re2c-0.13.5.tar.gz
			cd re2c-0.13.5/
			./configure --prefix=$install_directory

			make -j $cpu
			make install
			cd ..
			green "Installation of re2c Successfully"
		else
			error "Installation of re2c Aborted"
		fi
	fi
}

install_liba52()
{
	if $installAll ; then
		wget http://liba52.sourceforge.net/files/a52dec-0.7.4.tar.gz
		tar -xvzf a52dec-0.7.4.tar.gz
		cd a52dec-0.7.4/
		./bootstrap
		ARCh=`getconf LONG_BIT`
		#64bit processor bug fix
		if [[ $ARCh == '64' ]];then
			./configure --prefix=$install_directory --enable-shared 'CFLAGS=-fPIC'
		else
			./configure --prefix=$install_directory  --enable-shared
		fi

		make -j $cpu
		make install
		cd ..
	else
		printf "Would you like install liba52 [libaudio] ? [y/N]: "
		read liba52in
		if [ "$liba52in" = "y" -o "$liba52in" = "Y" ]; then
			wget http://liba52.sourceforge.net/files/a52dec-0.7.4.tar.gz
			tar -xvzf a52dec-0.7.4.tar.gz
			cd a52dec-0.7.4/
			./bootstrap
			ARCh=`getconf LONG_BIT`
			#64bit processor bug fix
			if [[ $ARCh == '64' ]];then
				./configure --prefix=$install_directory --enable-shared 'CFLAGS=-fPIC'
				
			else
				./configure --prefix=$install_directory  --enable-shared
			fi

			make -j $cpu
			make install
			cd ..
			green "Installation of liba52 Successfully"
		else
			error "Installation of liba52 Aborted"
		fi
	fi
}

delete_package_sources()
{
	counter() {
		for i in 5 4 3 2 1; do
			sleep 1 && echo -n "$i "
		done
	}

	color() {
		BLUE="\033[01;34m"
		NORMAL="\e[0;0m"
		case $1 in
			blue) printf "$BLUE" ;;
			normal) echo -ne "$NORMAL" ;;
		esac
	}
	
	if $installAll ; then
		rm -vrf libwmf*
		rm -vrf flvtool*
		rm -vrf lame*
		rm -vfr essential*
		rm -vrf libogg*
		rm -vrf libvorbis*
		rm -vrf vorbis-tools*
		rm -vrf libtheora*
		rm -vrf amrnb*
		rm -vrf amrwb*
		rm -vrf a52dec-0.7.4*
		rm -vrf faac*
		rm -vrf faad2*
		rm -vrf yasm*
		rm -vrf nasm*
		rm -vrf xvid*
		rm -vrf x264*
		rm -vrf re2c*
		rm -vrf mplayer*
		rm -vrf ffmpeg/*
		rm -vrf ffmpeg*
		rm -vrf all-20*
		rm -vrf gpac*
		rm -vrf ffms2*
		rm -vrf ffms1*
		rm -vrf mkvtoolnix*
		rm -vrf mediaInstall/*
		delete_main_programms_and_libs
	else	
		printf "We will delete old dependencies aka Libraries ? [y/N]: "
		read del
		if [ "$del" = "y" -o "$del" = "Y" ]; then
			color blue && printf "Starting delete Packages in: " && counter
			color normal && echo ""
		
			#Remove Packages for Issues
			rm -vrf libwmf*
			rm -vrf flvtool*
			rm -vrf lame*
			rm -vfr essential*
			rm -vrf libogg*
			rm -vrf libvorbis*
			rm -vrf vorbis-tools*
			rm -vrf libtheora*
			rm -vrf amrnb*
			rm -vrf amrwb*
			rm -vrf a52dec-0.7.4*
			rm -vrf faac*
			rm -vrf faad2*
			rm -vrf yasm*
			rm -vrf nasm*
			rm -vrf xvid*
			rm -vrf x264*
			rm -vrf re2c*
			rm -vrf mplayer*
			rm -vrf ffmpeg/*
			rm -vrf ffmpeg*
			rm -vrf all-20*
			rm -vrf gpac*
			rm -vrf ffms2*
			rm -vrf ffms1*
			rm -vrf mkvtoolnix*
			rm -vrf mediaInstall/*
			
			printf "Do you want Delete Main programms and Libs ? [y/N]: "
			read delmainlibs
			if [ "$delmainlibs" = "y" -o "$delmainlibs" = "Y" ]; then
				delete_main_programms_and_libs
				green "Main Programms and Libs Successfully Deleted"
			fi
		else
			error "It is higly recomended that you delete this files for beware of troubles after install"
		fi
	fi
}

delete_main_programms_and_libs()
{
	rm -vrf /lib/liba52*
	rm -vrf /lib/libamr*
	
	rm -vrf /lib/libdha*
	rm -vrf /lib/libfaac*
	rm -vrf /lib/libfaad*
	rm -vrf /lib/libmp3lame*
	rm -vrf /lib/libmp4v2*
	rm -vrf /lib/libogg*
	rm -vrf /lib/libtheora*
	rm -vrf /lib/libvorbis*
	rm -vrf /usr/lib/liba52*
	rm -vrf /usr/lib/libamr*
	rm -vrf /usr/lib/libavcodec*
	rm -vrf /usr/lib/libavformat*
	rm -vrf /usr/lib/libavutil*
	rm -vrf /usr/lib/libdha*
	rm -vrf /usr/lib/libfaac*
	rm -vrf /usr/lib/libfaad*
	rm -vrf /usr/lib/libmp3lame*
	rm -vrf /usr/lib/libmp4v2*
	rm -vrf /usr/lib/libogg*
	rm -vrf /usr/lib/libtheora*
	rm -vrf /usr/lib/libvorbis*

	rm -vrf /usr/local/lib/liba52*
	rm -vrf /usr/local/lib/libamr*
	rm -vrf /usr/local/lib/libavcodec*
	rm -vrf /usr/local/lib/libavformat*
	rm -vrf /usr/local/lib/libavutil*
	rm -vrf /usr/local/lib/libdha*
	rm -vrf /usr/local/lib/libfaac*
	rm -vrf /usr/local/lib/libfaad*
	rm -vrf /usr/local/lib/libmp3lame*
	rm -vrf /usr/local/lib/libmp4v2*
	rm -vrf /usr/local/lib/libogg*
	rm -vrf /usr/local/lib/libtheora*
	rm -vrf /usr/local/lib/libvorbis*
	unlink /usr/bin/ffmpeg >/dev/null 2>&1
	unlink /usr/local/bin/ffmpeg >/dev/null 2>&1
	unlink /usr/bin/x264 >/dev/null 2>&1
	unlink /usr/local/bin/x264 >/dev/null 2>&1
	unlink /usr/bin/lame >/dev/null 2>&1
	unlink /usr/local/bin/lame >/dev/null 2>&1
	unlink /usr/bin/mkvmerge >/dev/null 2>&1
	unlink /usr/local/bin/mkvmerge >/dev/null 2>&1
	unlink /usr/bin/MP4Box >/dev/null 2>&1
	unlink /usr/local/bin/MP4Box >/dev/null 2>&1
	unlink /usr/bin/faac >/dev/null 2>&1
	unlink /usr/local/bin/faac >/dev/null 2>&1
	unlink /bin/mplayer >/dev/null 2>&1
	unlink /usr/bin/mplayer >/dev/null 2>&1
	unlink /usr/local/bin/mplayer >/dev/null 2>&1
	unlink /bin/mencoder >/dev/null 2>&1
	unlink /usr/bin/mencoder  >/dev/null 2>&1
	unlink /usr/local/bin/mencoder >/dev/null 2>&1
	unlink /bin/flvtool2 >/dev/null 2>&1
	unlink /usr/bin/flvtool2 >/dev/null 2>&1
	unlink /usr/local/bin/flvtool2 >/dev/null 2>&1
}

install_mkvtoolnix()
{
	if $installAll ; then
		wget http://www.bunkus.org/videotools/mkvtoolnix/sources/mkvtoolnix-4.8.0.tar.bz2
		tar xjvf mkvtoolnix-4.8.0.tar.bz2
		cd mkvtoolnix-4.8.0/
		./autogen.sh
		./configure --prefix=$install_directory

		./drake
		./drake install
		cd ..
	else
		printf "Would you like install mkvtoolnix ? [y/N]: "
		read mkvtoolnixin
		if [ "$mkvtoolnixin" = "y" -o "$mkvtoolnixin" = "Y" ]; then
			wget http://www.bunkus.org/videotools/mkvtoolnix/sources/mkvtoolnix-4.8.0.tar.bz2
			tar xjvf mkvtoolnix-4.8.0.tar.bz2
			cd mkvtoolnix-4.8.0/
			./autogen.sh
			./configure --prefix=$install_directory

			./drake
			./drake install
			cd ..
			green "Installation of mkvtoolnix Successfully"
		fi
	fi
}

install_libschroedinger()
{
	if $installAll ; then
		  wget http://code.entropywave.com/download/orc/orc-0.4.14.tar.gz
		  tar xzvf orc-0.4.14.tar.gz
		  cd orc-0.4.14/
		  ./autogen.sh
		  ./configure --prefix=$install_directory
		  make
		  make install
		  cd ..
                wget http://diracvideo.org/download/schroedinger/schroedinger-1.0.10.tar.gz
		  tar xzvf schroedinger-1.0.10.tar.gz
                cd schroedinger-1.0.10/
		  ./autogen.sh
                ./configure --prefix=$install_directory
		  make
                make install
                cd ..               
        else
                printf "Would you like install libschroedinger library ? [y/N]:"
                read libschin
                if [ "$libschin" = "y" -o "$libschin" = "Y" ]; then
			wget http://code.entropywave.com/download/orc/orc-0.4.14.tar.gz
		  	tar xzvf orc-0.4.14.tar.gz
		  	cd orc-0.4.14/
		  	./autogen.sh
		  	./configure --prefix=$install_directory
		  	make
		  	make install
		  	cd ..
                     wget http://diracvideo.org/download/schroedinger/schroedinger-1.0.10.tar.gz
		  	tar xzvf schroedinger-1.0.10.tar.gz
                	cd schroedinger-1.0.10/
		  	./autogen.sh
                	./configure --prefix=$install_directory
		  	make
                	make install
                	cd ..
                    green "Installation of libschroedinger library Successfully"
                fi
        fi
}

welcome
install_all
add_media_sources
delete_package_sources
install_dev_dependencies
install_dir
install_codecs
install_re2c
install_liba52
install_flvtool
install_lame
install_gpac
install_libogg
install_libvorbis
install_vorbisTools
install_libtheora
install_xvid
install_faac
install_faad2
install_x264
install_libschroedinger
install_ffmpeg
install_ffmpegsource
install_mplayer